<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 头部表单 -->
      <el-form :inline="true" :model="formData" class="demo-form-inline">
        <!-- 栅栏布局 -->
        <el-row>
          <el-col :span="21" >
            <el-form-item label="关键字" class="words">
              <el-input
                v-model="formData.keyword"
                placeholder="根据文章标题搜索"
              ></el-input>
            </el-form-item>

            <el-form-item label="状态" class="stt">
              <el-select
                v-model="formData.conditionSearch"
                placeholder="请选择"
              >
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <!-- 朴素按钮 -->
              <el-button plain @click="clearKey"  size="small">清除</el-button>
              <!-- 默认按钮 -->
              <!-- 搜索时调用获取文章列表接口 -->
              <el-button type="primary" @click="getArticles"  size="small">搜索</el-button>
            </el-form-item>
          </el-col>
          <!-- 新增技巧 -->
          <el-col :span="3">
            <el-button type="success" icon="el-icon-edit" @click="articleBody"  size="small"> 新增技巧</el-button>
          </el-col>
        </el-row>
      </el-form>

      <!-- 数据提示 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>

      <!-- 表格 :index="indexMethod" -->
      <el-table :data="directorysData" style="width: 100%">
        <el-table-column type="index" label="序号" width="100px" >
          <template #default='scope'>
            {{(page - 1) * pagesize + scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="260px" >
          <!-- 表格内存放icon -->
          <template #default="{row}">
            {{row.title}}
            <div v-if="row.videoURL" class="el-icon-film" style="color:blue;font-size:16px;cursor:pointer" @click="openVideo(row.videoURL)"/>

          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="80px">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="100px">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="200px">
          <template #default="{row}">
            {{row.createTime|parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="100px" :formatter="formatter">
        </el-table-column>

        <el-table-column  label="操作" width="180%">
            <template #default="{row }">
              <el-button
                type="text"
                size="small"
                @click="preview(row)"
                >预览</el-button
              >

              <el-button
                @click="handleSwitch(row)"
                type="text"
                size="small"
                >{{row.state===1?'禁用':'启用'}}</el-button
              >

              <el-button
                type="text"
                size="small"
                :disabled="row.state === 1"
                @click="editForm(row)"

                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                :disabled="row.state === 1"
                @click="deleteItem(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>

      </el-table>

      <!-- 分页 -->
      <div >
        <el-row type="flex" class="row-bg" justify="end">
          <!--
            size-change： pageSize 改变时会触发
            current-change：page改变时会触发
            page-size每页显示条目个数
           -->
          <el-pagination
            background
            :total="total"
            :current-page="page"
            :page-sizes="[10, 5, 20, 25]"
            :page-size="pagesize"
            layout=" prev, pager, next,sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </el-row>
      </div>
    </el-card>
    <!-- 预览 -->
    <articles-preview
    :previewDialog.sync = "previewDialog"
    :previewDate="currentNode"
    >

    </articles-preview>
      <!-- 新增技巧 -->
      <!-- 弹框出现 父传子弹框出现-->
      <articles-add
      :showDialog="showDialog"
      :nodeData="currentNode"
      @closeDialogFn="closeDialog"
      @addSuccess="resetSuccess"
      ref="addCom"

      >
      </articles-add>
      <div v-if="videoUrl" class="video" >
              <div @click="closeVideo()" class="video-ico">
                <span class="el-icon-close" ></span>
              </div>
              <div class="video-box">
                <meta name="referrer" content="never" />
                <video :src="videoUrl" controls autoplay></video>
              </div>
            </div>
  </div>
</template>

<script>
import { listApi, changeState, remove } from '@/api/hmmm/articles.js'
import articlesAdd from '@/module-hmmm/components/articles-add.vue'
import articlesPreview from '@/module-hmmm/components/articles-preview.vue'
import { status } from '@/api/hmmm/constants.js'

export default {
  // 头部数据
  data () {
    return {
      formData: {
        keyword: '', // 标签名称
        conditionSearch: '' // 标签状态
      },
      total: 0,
      directorysData: [], // 渲染树的数组
      page: 1, // 当前页
      pagesize: 10, // 每页条数
      showPreview: false,
      currentNode: {}, // 当前行
      showDialog: false,
      stu: status,
      previewDialog: false,
      showVideo: false,
      videoUrl: null
    }
  },
  created () {
    this.getArticles()
  },
  components: {
    articlesPreview,
    articlesAdd
  },
  methods: {
    // 获取文章列表
    async getArticles () {
      const pageData = {
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.formData.keyword
      }
      // 传状态
      if (this.formData.conditionSearch) {
        pageData.state = this.formData.conditionSearch
      }
      const { data } = await listApi(pageData)
      this.directorysData = data.items
      this.total = data.counts
    },

    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      // console.log(this.pagesize)
      this.page = 1
      this.getArticles()
    },
    resetSuccess () {
      this.page = 1
      this.getArticles()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getArticles()
    },
    // 枚举
    formatter (row, column, cellValue, index) {
      const obj = this.stu.find((item) => item.value === cellValue)
      if (obj) {
        return (`已${obj.label}`)
      } else {
        return ('未知')
      }
      // return obj ? obj.label : '未知'
    },
    // 分页序号问题
    // indexMethod (index) {
    //   return (this.page - 1) * this.pagesize + index + 1
    // },
    // 点击清除  清空关键字
    clearKey () {
      this.formData.keyword = ''
      this.formData.conditionSearch = ''
    },
    // 预览
    preview (row) {
      this.previewDialog = true
      this.currentNode = row
      // console.log(this.currentNode)
    },
    // 点击新增技巧显示弹框
    articleBody () {
      this.showDialog = true
    },
    // 关闭弹窗
    closeDialog (val) {
      this.showDialog = val
    },
    // 启用禁用切换  状态传切换后的
    async handleSwitch (row) {
      const switchData = {
        id: row.id,
        state: row.state === 1 ? 0 : 1
      }
      await changeState(switchData)
      this.$message.success('操作成功！')
      this.getArticles()
    },
    // 修改文章
    async editForm (row) {
      this.showDialog = true

      this.currentNode = row
      this.$nextTick(() => {
        this.$refs.addCom.detail()
      })
    },
    // 删除文章
    deleteItem (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('删除成功')
        this.getArticles()
      }).catch(() => {})
    },
    openVideo (row) {
      this.videoUrl = row
    },
    closeVideo () {
      this.videoUrl = null
    }
  }
}
</script>

<style lang="scss" scoped>
.stt {
  margin-left: 50px;
}
.container{
  padding-left: 10px;
  padding-top: 10px;
}
.words{
  padding-left: 20px;
}
.video {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .video-ico {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .video-box {
    width: 600px;
    height: 400px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
